package middlewares

import (
	"Im/utils"
	"github.com/gin-gonic/gin"
)

func AuthCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if token == "" {
			token = c.GetHeader("sec-websocket-protocol")
		}
		userClaims, err := utils.ParseToken(token)
		if err != nil {
			c.AbortWithStatusJSON(403, gin.H{
				"msg": "用户认证不通过",
			})
			return
		}
		c.Set("user_claims", userClaims)
		c.Next()
	}
}
